﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Common;
using School.Entities;


namespace School.Pages.Admission
{
    public partial class AddStudentsToClass : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1024) == true)
                {
                    if (!IsPostBack)
                    {
                        LstCampuses.DataSource = Structure.GetCampuses();
                        LstCampuses.DataTextField = "Name";
                        LstCampuses.DataValueField = "ID";
                        LstCampuses.DataBind();

                        LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
                        LstCertificates.DataValueField = "Id";
                        LstCertificates.DataTextField = "Name";
                        LstCertificates.DataBind();

                        LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
                        LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
                        LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
                        lstClasses.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الفصل" });
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void LstCampuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
            LstCertificates.DataValueField = "Id";
            LstCertificates.DataTextField = "Name";
            LstCertificates.DataBind();

            LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
        }

        protected void LstCertificates_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            LstStages.DataValueField = "Id";
            LstStages.DataTextField = "Name";
            LstStages.DataBind();
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
        }

        protected void LstStages_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
        }

        protected void LstGrades_SelectedIndexChanged(object sender, EventArgs e)
        {
            var classes = Structure.GetClassesByGrade(long.Parse(LstGrades.SelectedValue));
            lstClasses.DataSource = classes;
            lstClasses.DataValueField = "Id";
            lstClasses.DataTextField = "Name";
            lstClasses.DataBind();
            lstClasses.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الفصل" });

            if (classes.Count() > 0)
                lstClasses.SelectedValue = classes.First().Id.ToString();

            FillStudentsGrid();
        }



        protected void FillStudentsGrid()
        {
            GridView1.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue)).Select(x => new { Name = x.Ar_FirstName + " " + x.Ar_SecondName + " " + x.Ar_ThirdNmae + " " + x.Ar_FourthName, Number = x.RegistrationNumber, x.Id });
            GridView1.DataBind();
        }

        protected Boolean InClass(object id)
        {
            SchoolEntities db = new SchoolEntities();            
            var cYear = db.ORG_EductionalYear.Single(x => x.IsCurrent == true).Id;
            var cls = long.Parse(lstClasses.SelectedValue);

            try
            {
                var sId = long.Parse(id.ToString());                
                var st = db.TE_Class_Student.Single(x => x.EductionalYear_Id == cYear && x.Student_Id == sId && x.Class_Id == cls);
                return true;
            }
            catch
            {
                return false;
            }
        }

        protected void lstClasses_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillStudentsGrid();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SchoolEntities db = new SchoolEntities();
            var cYear = db.ORG_EductionalYear.Single(x => x.IsCurrent == true).Id;
            var cls = int.Parse(lstClasses.SelectedValue);

            foreach (GridViewRow x in GridView1.Rows)
            {
                long id = long.Parse(((Label)x.FindControl("lblStudentId")).Text);
                Boolean inCLass = ((CheckBox)x.FindControl("ChkIsInClass")).Checked;
                
                if (inCLass)
                {
                    var currentClass = from y in db.TE_Class_Student where y.Student_Id == id && y.EductionalYear_Id == cYear select y;
                    if(currentClass.Count() > 0)
                        db.DeleteObject(currentClass.First());                    

                    db.AddToTE_Class_Student(new TE_Class_Student() { Class_Id = cls, Student_Id = id, EductionalYear_Id = cYear });
                }
            }

            db.SaveChanges();
        }
    }
}